package com.yyk.yiliao.base;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2018/9/18 0018.
 */

public abstract class BaseFragment extends BaseLazyLoadFragment {

    public static final String LAST_ACT = "LAST_ACT";
    public static final String BUNDLE = "TAG_BUNDLE";

    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        }
        /*---------------------------------------------------------------------------------------------------*/
        /**
         * 这个不知道有几八鸟用
         */
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        /*---------------------------------------------------------------------------------------------------*/

        unbinder = ButterKnife.bind(this, rootView);
        initView();
        initData();

        return rootView;

    }


    public abstract int getLayoutId();

        public abstract void initView();

        public abstract void initData();

    /* --------------------------------------------------------------------------------------------------------------------------------*/

    protected Bundle getBundle() {
        if (getActivity().getIntent().hasExtra(BUNDLE)) {
            return getActivity().getIntent().getBundleExtra(BUNDLE);
        }
        return null;
    }

    /**
     * 优化跳转  递归用法
     *
     * @param clazz
     */
    public void startAct(Class clazz) {
        startAct(clazz, null);
    }


    /**
     * @see #startAct(Intent, Class, Bundle)
     */
    protected void startAct(Class<?> cls, Bundle bundle) {
        startAct(null, cls, bundle);
    }

    /**
     * 启动Activity
     */
    protected void startAct(Intent intent, Class<?> cls, Bundle bundle) {
        if (intent == null)
            intent = new Intent();
        if (bundle != null)
            intent.putExtra(BUNDLE, bundle);
        intent.putExtra(LAST_ACT, this.getClass().getSimpleName());
        intent.setClass(getActivity(), cls);
        startActivity(intent);
    }

    /**
     * 不可以传值 可以控制当前界面是否finsh
     *
     * @param cls
     * @param isFinish
     */
    public void startAct(Class cls, boolean isFinish) {
        startAct(cls, null);
        if (isFinish) {
            getActivity().finish();
        }
    }

    /**
     * 可以传值 并且可以控制当前界面是否finsh
     *
     * @param cls
     * @param isFinish
     * @param bundle
     */
    public void startAct(Class cls, boolean isFinish, Bundle bundle) {
        startAct(null, cls, bundle);
        if (isFinish) {
            getActivity().finish();
        }
    }

    /* --------------------------------------------------------------------------------------------------------------------------------*/

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

}
